package com.mystudy.concurrent.designModel.singleThreadModel;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * TODO: 资源类  单线程访问类
 * 优点： 写操作的时候不会导致错乱，
 * 缺点：多线程读取的时候，性能低
 * @author acy
 * @since 2019/05/28 19:23
 */
@Getter
@Setter
public class Gate {
	private int counter = 0;
	private String name = "Nobody";
	private String address = "NowWhere";

	public synchronized void pass(String name, String address) {
		this.counter++;
		this.name = name;
		this.address = address;
		verify();
	}

	private void verify() {
		if (this.name.charAt(0) != this.address.charAt(0)){
			System.out.println("**********BROKEN**********"+toString());
		}
	}

	@Override
	public synchronized String toString() {
		return "Gate{" +
				"counter=" + counter +
				", name='" + name + '\'' +
				", address='" + address + '\'' +
				'}';
	}
}
